import {
  loanEndRepayCount,
  repayRateCount,
  loanEndRepayTable,
  overdueRepayCount,
  overdueRepayTrend,
  overdueRepayTable,
  totalRepayCount,
  totalRepayTable,

  loanEndRepayCountForXBeauty,
  repayRateCountForXBeauty,
  loanEndRepayTableForXBeauty,
  overdueRepayCountForXBeauty,
  overdueRepayTrendForXBeauty,
  overdueRepayTableForXBeauty,
  totalRepayCountForXBeauty,
  totalRepayTableForXBeauty,
} from '../../services/channel/api';


export default {
  namespace: 'backStatistics',

  state: {
    loading: true,
    repayTableData: {
      list: [],
      pagination:{

      }
    },
    overdueTableData: {
      list: [],
      pagination:{
      }
    },
    totalRepayTableData: {
      list: [],
      pagination:{
      }
    },
  },

  effects: {
    *fetchLoanEndRepayCount({payload}, { call, put }) {
      const response = yield call(loanEndRepayCount,payload);
      if (response && response.code == 200) {
        switch (payload.userType){
          case 'all-user':
            yield put({
              type: 'save',
              payload: {abData: response.data},
            });
            break;
          case 'new-user':
            yield put({
              type: 'save',
              payload: {nbData: response.data},
            });
            break;
          case 'old-user':
            yield put({
              type: 'save',
              payload: {obData: response.data},
            });
            break;
        }

      }
    },

    *fetchRepayRateCount({payload,callback}, { call, put }) {
      const response = yield call(repayRateCount,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {repayTrendData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchLoanEndRepayTable({payload,callback}, { call, put }) {
      const response = yield call(loanEndRepayTable,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            repayTableData: {
              list: response.data.list,
              pagination:{
                current: response.data.currentPage,
                total: response.data.totalCount,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    *fetchOverdueRepayCount({payload}, { call, put }) {
      const response = yield call(overdueRepayCount,payload);
      if (response && response.code == 200) {
        switch (payload.userType){
          case 'all-user':
            yield put({
              type: 'save',
              payload: {aoData: response.data},
            });
            break;
          case 'new-user':
            yield put({
              type: 'save',
              payload: {noData: response.data},
            });
            break;
          case 'old-user':
            yield put({
              type: 'save',
              payload: {ooData: response.data},
            });
            break;
        }
      }
    },

    *fetchOverdueRepayTrend({payload,callback}, { call, put }) {
      const response = yield call(overdueRepayTrend,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {overdueTrendData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchOverdueRepayTable({payload,callback}, { call, put }) {
      const response = yield call(overdueRepayTable,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            overdueTableData: {
              list: response.data.list,
              pagination:{
                current: response.data.currentPage,
                total: response.data.totalCount,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    *fetchTotalRepayCount({payload,callback}, { call, put }) {
      const response = yield call(totalRepayCount,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {totalRepayCountData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchTotalRepayTable({payload,callback}, { call, put }) {
      const response = yield call(totalRepayTable,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            totalRepayTableData: {
              list: response.data.list,
              pagination:{
                current: response.data.currentPage,
                total: response.data.totalCount,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    //想美
    *fetchLoanEndRepayCountForXBeauty({payload}, { call, put }) {
      const response = yield call(loanEndRepayCountForXBeauty,payload);
      if (response && response.code == 200) {
        switch (payload.userType){
          case 'all-user':
            yield put({
              type: 'save',
              payload: {abData: response.data},
            });
            break;
          case 'new-user':
            yield put({
              type: 'save',
              payload: {nbData: response.data},
            });
            break;
          case 'old-user':
            yield put({
              type: 'save',
              payload: {obData: response.data},
            });
            break;
        }

      }
    },

    *fetchRepayRateCountForXBeauty({payload,callback}, { call, put }) {
      const response = yield call(repayRateCountForXBeauty,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {repayTrendData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchLoanEndRepayTableForXBeauty({payload,callback}, { call, put }) {
      const response = yield call(loanEndRepayTableForXBeauty,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            repayTableData: {
              list: response.data.list,
              pagination:{
                current: response.data.currentPage,
                total: response.data.totalCount,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    *fetchOverdueRepayCountForXBeauty({payload}, { call, put }) {
      const response = yield call(overdueRepayCountForXBeauty,payload);
      if (response && response.code == 200) {
        switch (payload.userType){
          case 'all-user':
            yield put({
              type: 'save',
              payload: {aoData: response.data},
            });
            break;
          case 'new-user':
            yield put({
              type: 'save',
              payload: {noData: response.data},
            });
            break;
          case 'old-user':
            yield put({
              type: 'save',
              payload: {ooData: response.data},
            });
            break;
        }
      }
    },

    *fetchOverdueRepayTrendForXBeauty({payload,callback}, { call, put }) {
      const response = yield call(overdueRepayTrendForXBeauty,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {overdueTrendData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchOverdueRepayTableForXBeauty({payload,callback}, { call, put }) {
      const response = yield call(overdueRepayTableForXBeauty,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            overdueTableData: {
              list: response.data.list,
              pagination:{
                current: response.data.currentPage,
                total: response.data.totalCount,
              }
            }
          },
        });
        if (callback) callback();
      }
    },

    *fetchTotalRepayCountForXBeauty({payload,callback}, { call, put }) {
      const response = yield call(totalRepayCountForXBeauty,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {totalRepayCountData: response.data},
        });
        if (callback) callback();
      }
    },

    *fetchTotalRepayTableForXBeauty({payload,callback}, { call, put }) {
      const response = yield call(totalRepayTableForXBeauty,payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            totalRepayTableData: {
              list: response.data.list,
              pagination:{
                current: response.data.currentPage,
                total: response.data.totalCount,
              }
            }
          },
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
